package com.atwulidun.sparsearray;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ReadSparseArray {
    public static void main(String[] args) {
        // 从硬盘中读取保存的稀疏数组文件
        Object o = null;
        try (FileInputStream fileInputStream = new FileInputStream(
                "E:\\sparseArray\\sparseArray");
             ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
        ) {
            o = inputStream.readObject();
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }
        int[][] sparseArr = (int[][]) o;
        for (int[] ints : sparseArr) {
            System.out.printf("%d\t%d\t%d\t",ints[0],ints[1],ints[2]);
            System.out.println();
        }
        // 将稀疏数组转换为二维数组arr2
        int[][] arr2 = new int[sparseArr[0][0]][sparseArr[0][1]];
        for (int i = 1; i < sparseArr.length; i++) {
            arr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        for (int[] ints : arr2) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }
    }
}
